<template>
    <div class="detail">
        <div v-if="detailShow">
            <van-nav-bar :title="$t('benefit.title_two')" left-arrow fixed @click-left="_return"
                         :right-text="$t('benefit.title_three')" @click-right="showRecord" :z-index="555"></van-nav-bar>
            <div class="benefit-detail-main">
                <div class="benefit-detail-main-header">
                    <div class="benefit-detail-main-header-info">
                        <img :src="avatar" alt="">
                        <span>{{ $t('benefit.name_two') }}</span>
                    </div>
                    <button class="benefit-detail-main-header-btn" @click="handleShowKf">{{$t('benefit.btns')}}</button>
                </div>
                <div class="benefit-detail-main-new">
                    <div class="benefit-detail-main-card">
                        <h4 class="benefit-detail-main-title">{{ detail.title }}</h4>
                        <div class="benefit-detail-main-card-info">
                            <div class="benefit-detail-main-card-info-item" v-for="(item, key) in cardInfo" :key="key">
                                <p class="benefit-detail-main-card-info-item-value">{{ item.value }}</p>
                                <p class="benefit-detail-main-card-info-item-label">{{ item.label }}<template v-if="item.coin"><span style="color: #CBAF89;">{{ item.coin }}</span></template></p>
                            </div>
                        </div>
                    </div>
                    <div class="benefit-detail-main-promise">
                        <img :src="chen" alt="">
                        <div class="benefit-detail-main-promise-desc">
                            <p>{{ $t('benefit.promise_one') }}</p>
                            <p>{{ $t('benefit.promise_two') }}</p>
                        </div>
                    </div>
                    <div class="benefit-detail-main-story detail-main-item">
                        <div class="detail-main-item-header">{{ $t('benefit.story') }}</div>
                        <p v-html="detail.con1"></p>
                        <ul class="detail-main-item-list">
                            <van-image width="100" height="100" v-for="(item, key) in detail.con2" @click="showImagePreview(key,detail.con2)" :key="item" :src="item"/>
                        </ul>
                    </div>
                    <div class="benefit-detail-main-prove detail-main-item">
                        <div class="detail-main-item-header">{{ $t('benefit.material') }}</div>
                        <ul class="detail-main-item-list">
                            <van-image width="100" height="100" v-for="(item, key) in detail.prove" @click="showImagePreview(key,detail.prove)" :key="item" :src="item"/>
                        </ul>
                    </div>
                    <div class="benefit-detail-main-paper detail-main-item">
                        <div class="detail-main-item-header">{{ $t('benefit.paper') }}</div>
                        <ul class="detail-main-item-list">
                            <van-image width="100" height="100" v-for="(item, key) in detail.promise" @click="showImagePreview(key,detail.promise)" :key="item" :src="item"/>
                        </ul>
                    </div>
                    <div class="benefit-detail-main-statement">
                        <div class="benefit-detail-main-statement-content">
                            <h6>{{ $t('benefit.statement_one') }}</h6>
                            <p>{{ $t('benefit.statement_two') }}</p>
                            <p>{{ $t('benefit.statement_three') }}</p>
                        </div>
                        <div class="benefit-detail-main-statement-footer">
                            <span @click.stop="handleShowKf"><span class="icon">→</span><span>{{ $t('benefit.more') }}</span></span>
                            <span @click.stop="handleShowKf"><span class="icon">!</span><span>{{ $t('benefit.question') }}</span></span>
                        </div>
                    </div>
                </div>
                <div class="benefit-detail-main-footer">
                    <van-button class="benefit-detail-main-footer-btn" @click="handleConfirm">{{ $t('benefit.button_three') }}</van-button>
                </div>
            </div>
        </div>
        <div v-if="recordShow">
            <van-nav-bar :title="$t('benefit.title_three')" left-arrow fixed @click-left="_returns"></van-nav-bar>

            <div class="benefit-detail-record">
                <div class="benefit-detail-record-main">
                    <div class="benefit-detail-record-tip">
                        <div class="benefit-detail-record-card">
                            <div class="record-card-item">
                                <p class="record-card-item-value">{{str}}</p>
                                <p class="record-card-item-label">{{ $t('benefit.raise') }}<span style="color:#CBAF89;">BVES</span></p>
                            </div>
                            <div class="record-card-item">
                                <p class="record-card-item-value">{{recordList.length}}</p>
                                <p class="record-card-item-label">{{ $t('benefit.times') }}</p>
                            </div>
                        </div>
                    </div>
                    <div class="benefit-detail-record-user">
                        <div class="benefit-detail-record-user-header">{{ $t('benefit.account') }}</div>
                        <ul class="benefit-detail-record-user-list">
                            <li class="benefit-detail-record-user-item" v-for="item,index in recordList" :key="index">
                                <img :src="chen" alt="" class="benefit-detail-record-user-avatar">
                                <div class="benefit-detail-record-user-info">
                                    <p class="benefit-detail-record-user-phone">{{ item.invite_code }}</p>
                                    <p class="benefit-detail-record-user-desc">{{ $t('benefit.donate') }}{{ Number(item.donate_num) }}{{item.coin_name.toUpperCase()}}</p>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <donate v-if="donateShow" @_return="_returns" :child="child" :status="status"></donate>
        <service v-if="serviceShow" @_return="_returns"></service>
    </div>
</template>

<script>
    import donate from '@/components/welfare/donate'
    import service from '@/components/welfare/benefitKf'
    import { ImagePreview } from 'vant'
    export default {
        data() {
            return {
                avatar:require('../../assets/logos.jpg'),
                chen:require('../../assets/chengnuo.png'),
                cardInfo: [
                    {
                        label: this.$t('benefit.need'),
                        coin: '',
                        value: ''
                    },
                    {
                        label: this.$t('benefit.raise'),
                        coin: '',
                        value: ''
                    },
                    {
                        label: this.$t('benefit.times'),
                        value: ''
                    }
                ],
                detail:{},
                recordShow:false,
                detailShow:true,
                page:1,
                listNum:'',
                recordList:[],
                donateShow:false,
                serviceShow:false,
                status:'',
                str:0
            }
        },
        methods: {
            handleShowKf(){
                this.detailShow = false
                this.serviceShow = true
            },
            _return(){
                this.$emit('_return')
            },
            handleConfirm(){
                window.removeEventListener("scroll", this.reachBottom,true)
                this.detailShow = false
                this.donateShow = true
            },
            getData(){
                this.$api.get('welfare/welfaredetail',{welfare_id:this.child})
                    .then(res =>{
                        this.detail = res.detail
                        this.status = res.detail.status
                        this.detail.con2.forEach((item,index) =>{
                            this.detail.con2[index] = this.detail.web + item
                        })
                        this.detail.prove.forEach((item,index) =>{
                            this.detail.prove[index] = this.detail.web + item
                        })
                        this.detail.promise.forEach((item,index) =>{
                            this.detail.promise[index] = this.detail.web + item
                        })
                        this.cardInfo[0].value = Number(this.detail.allnum).toFixed(2)
                        this.cardInfo[0].coin = this.detail.cointype.toUpperCase()
                        this.cardInfo[1].value = Number(this.detail.aready).toFixed(2)
                        this.cardInfo[1].coin = this.detail.cointype.toUpperCase()
                        this.cardInfo[2].value = this.detail.num
                    })
            },
            showRecord(){
                this.recordShow = true
                this.detailShow = false
                window.addEventListener("scroll", this.reachBottom,true)
                this.getRecord()
            },
            getRecord(){
                this.$api.get('welfare/donaterecords',{bid:23,page:this.page})
                    .then(res =>{
                        this.listNum = res.detail.listNum
                        res.detail.list.forEach(item => this.recordList.push(item))
                        this.recordList.map(item =>this.str += Number(item.donate_num))
                        if(res.detail.list.length < 10){
                            window.removeEventListener("scroll", this.reachBottom,true)
                        }
                    })
            },
            _returns(){
                this.recordShow = this.donateShow = this.serviceShow = false
                this.detailShow = true
                this.recordList = []
                window.removeEventListener("scroll", this.reachBottom,true)
            },
            getScrollTop () {
                // 考虑到浏览器版本兼容性问题，解析方式可能会不一样
                return document.documentElement.scrollTop || document.body.scrollTop
            },
            getWinHeight () {
                // 浏览器可见内容高度 || 浏览器所有内容高度(考虑到浏览器版本兼容性问题，解析方式可能会不一样)
                return document.documentElement.clientHeight || document.body.clientHeight
            },
            getScrollHeight() {
                let bodyScrollHeight = 0
                let documentScrollHeight = 0
                if (document.body) {
                    bodyScrollHeight = document.body.scrollHeight
                }
                if (document.documentElement) {
                    documentScrollHeight = document.documentElement.scrollHeight
                }
                // 当页面内容超出浏览器可视窗口大小时，Html的高度包含body高度+margin+padding+border所以html高度可能会大于body高度
                return (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
            },
            isReachBottom () {
                const scrollTop = this.getScrollTop() // 获取滚动条的高度
                const winHeight = this.getWinHeight() // 一屏的高度
                const scrollHeight = this.getScrollHeight() // 获取文档总高度
                return scrollTop >= parseInt(scrollHeight) - winHeight
            },
            reachBottom() {
                if (this.isReachBottom()) {
                    if (this.listNum > 10) {
                        this.page ++
                        this.getRecord()
                    } else {
                        window.onscroll = null
                    }
                }
            },
            showImagePreview(position,images){
                const instance = ImagePreview({
                    images,
                    closeOnPopstate: true,
                    startPosition: typeof position === 'number' ? position : 0
                });
            }
        },
        created() {
            this.getData()
        },
        mounted() {

        },
        props:{
            child:{}
        },
        components:{
            donate,
            service
        }
    }
</script>

<style lang="stylus" scoped>
    [class*=van-hairline]::after{
        border: none;
    }
    bac = linear-gradient(90deg,rgba(42,42,42,1) 0%,rgba(46,46,46,1) 100%);
    .benefit-detail {

        &-main {
            padding: 56px 0;
            &-header {
                background: linear-gradient(180deg, #54d8b4, #26c196);;
                color: #1F1F1F;
                display: flex;
                justify-content: space-between;
                align-items: center;
                padding: 8px 10px;
                &-info {
                    img {
                        margin-right: 3px;
                        vertical-align: middle;
                        width 30px
                    }
                    span {
                        vertical-align: middle;
                    }
                }
                &-btn {
                    border: 1px solid #115f49;
                    padding: 8px 16px;
                    background-color: rgba(0,0,0,0);
                    color: #115f49;
                    border-radius 5px
                }
            }

            &-new{
                width 94%
                margin 5px auto
            }
            &-card {
                padding: 10px;
                background rgba(0,0,0,0);
                border-radius 5px
                border 1px solid #efefef
                &-info {
                    margin-top: 15px;
                    display: flex;
                    padding: 20px 10px;
                    border-radius 5px
                    &-item {
                        flex: 1;
                        box-sizing: border-box;
                        text-align: center;
                        margin-right: 5px;
                        &-label {
                            font-size: 12px;
                            line-height: 18px;
                            color: #898989;
                            margin-top: 4px;
                        }
                        &-value {
                            font-size: 20px;
                            line-height: 28px;
                            color: #2fc49a;
                        }
                    }
                }
            }

            &-title {
                font-size: 18px;
                color: #666;
                line-height: 28px;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
            }

            &-promise {
                background: linear-gradient(180deg, #54d8b4, #26c196);
                display: flex;
                align-items: center;
                border-radius 5px
                margin-top 10px
                padding 15px 5px
                img {
                    width 35px
                }
                &-desc {
                    flex: 1;
                    margin-left: 8px;
                    p:first-child {
                        color: #115f49;
                        line-height: 24px;
                    }
                    p:last-child {
                        font-size: 14px;
                        line-height: 20px;
                        color: #fff;
                    }
                }
            }

            .detail-main-item {
                margin-top: 10px;
                padding: 15px;
                background: linear-gradient(180deg, #54d8b4, #26c196);
                box-shadow:0px 6px 12px 0px rgba(0,0,0,0.5);
                border-radius 5px
                &-header {
                    color: #115f49;
                    line-height: 24px;
                    margin-bottom: 8px;
                }
                p{
                    font-size 14px
                    line-height 20px
                    color #fff
                }
                &-list {
                    display flex
                    justify-content left
                    flex-flow row wrap
                    .van-image{
                        margin 3px
                    }
                }
            }

            &-statement {
                padding: 15px;
                background linear-gradient(180deg, #54d8b4, #26c196)
                box-shadow:0px 6px 12px 0px rgba(0,0,0,0.5);
                border-radius 5px
                margin-top 10px
                &-content {
                    font-size: 14px;
                    color: #115f49;
                    h4 {}
                    p + p {
                        margin-top: 20px;
                    }
                }
                &-footer {
                    margin-top: 10px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    color: #fff;
                    .icon {
                        display: inline-block;
                        width: 20px;
                        height: 20px;
                        font-size: 12px;
                        text-align: center;
                        line-height: 20px;
                        border-radius: 20px;
                        margin-right: 4px;
                        color: #fff;
                        background-color: #CBAF89;
                        vertical-align: middle;
                    }
                }
            }

            &-footer {
                position: fixed;
                bottom: 0;
                left: 0;
                width: 100%;
                box-sizing: border-box;
                box-shadow: 0 -2px 10px 0 rgba(0,0,0,0.3);

                &-btn {
                    background: linear-gradient(180deg, #54d8b4, #26c196);
                    color: #fff;
                    width: 100%;
                }
            }
        }

        &-record {
            padding-top: 56px;
            width 94%
            margin 0 auto
            &-main {

            }
            &-tip{
                background: linear-gradient(180deg, #54d8b4, #26c196);
                border-radius 5px
                padding: 20px 10px
            }
            &-card {
                display: flex;
                padding 20px 10px
                background:rgba(255,255,255,1);
                border-radius:5px;
                .record-card-item {
                    flex: 1;
                    text-align: center;

                    &-label {
                        font-size: 12px;
                        line-height: 18px;
                        margin-top: 4px;
                        color: #898989;
                    }
                    &-value {
                        color: #115f49;
                        font-size: 20px;
                        line-height: 28px;
                    }
                }
            }

            &-user {
                color: #D8D8D8;
                &-header {
                    padding-left: 15px;
                    height: 55px;
                    line-height: 55px;
                }

                &-item {
                    padding:10px 15px;
                    margin-bottom: 10px;
                    background: linear-gradient(90deg,rgba(42,42,42,1) 0%,rgba(46,46,46,1) 100%);
                    display: flex;
                    align-items: center;
                    border-radius 5px
                }

                &-avatar {
                    width: 35px;
                    height: 35px;
                    flex: 0 0 35px;
                    margin-right: 8px;
                }
                &-info {
                    flex: 1;
                }
                &-phone {
                    line-height: 22px;
                    color #D8D8D8
                }
                &-desc {
                    color: #898989;
                    font-size: 14px;
                }
            }
        }

        .benefit-detail-confirm.van-popup {
            height: 50%;
            background-color: #fff !important;
        }
        &-confirm {
            padding: 0 20px;
            box-sizing: border-box;
            color: #071E57;
            &-header {
                text-align: center;
                height: 65px;
                line-height: 65px;
            }

            &-item {
                display: flex;
                align-items: center;
                justify-content: space-between;
                height: 60px;
                line-height: 60px;
                font-size: 14px;

                &-label {
                    opacity: 0.6;
                }

                &-inp {
                    height: 20px;
                    border: none;
                    text-align: right;
                    margin-right: 9px;
                }

                & + & {
                    border-top: 1px solid rgba(0, 0, 0, 0.1);
                }
            }

            &-btn {
                position: absolute;
                bottom: 15px;
                left: 15px;
                width: calc(100% - 40px);
                display: flex;
                justify-content: space-between;
                button {
                    height: 44px;
                    border: none;
                    width: calc(50% - 5px);
                    &:first-child {
                        background-color: #F2F5F6;
                        color: #989898;
                    }
                    &:last-child {
                        background-color: #3085DE;
                        color: #fff;
                    }
                }
            }
        }
    }
</style>